/**
 * Created by RickyTT on 14-1-25.
 */
var db = require('./db');
var iMysql = require('./sqldb');

function UserInfo (userInfo){
    this.id = userInfo.id;
    this.userId = userInfo.userId;
    this.sex  = userInfo.sex;
    this.email = userInfo.email;
    this.age = userInfo.age;
    this.birthday = userInfo.birthday;
    this.updateTime = userInfo.updateTime;
}

module.exports = UserInfo;

UserInfo.getByUserId = function getByUserId(userId,callback){
    db.sql('select * from userInfo where userId = ?',userId,function(err,result){
        if(err){
            callback(err,null);
            console.log(err);
        }else{
            var userInfo = new UserInfo(result[0]);
            callback(1,userInfo);
        };
    });
};


UserInfo.insert = function insert(userId,sex,email,age,birthday,callback){
    var trans = iMysql.transaction();
    var birth;
    if(birthday){
        birth = birthday;
    }else{
        birth = '1970-01-01';
    }
    trans.sql('insert into userInfo (userId,sex,email,age,birthday) values ('+userId+','+sex+',"'+email+'",'+age+',"'+birth+'")',function(err,result){
        if(err){
            callback(err,null);
            console.log(err);
        }else{
            trans.commit(function(err){
                if(err){
                    callback(err,null);
//                    console.log(err);
                }else{
                    callback(err,result);
//                    console.log(result);
                };
            });
        };
    });
};

UserInfo.deleteByUserId = function deleteByUserId(userId,callback){
    var trans = iMysql.transaction();
    trans.sql('delete from userInfo where userId = '+userId+'',function(err,result){
        if(err){
            callback(err,null);
            throw err;
        }else{
            trans.commit(function(err){
                if(err){
                    callback(err,null);
//                    console.log(err);
                }else{
                    callback(err,result);
//                    console.log(result);
                };
            });
        };
    });
};